import requests
import re
import urllib
from bs4 import BeautifulSoup



def get_html(url):
    url_name = []
    video_name = []
    hr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome"
                        "/70.0.3538.102 Safari/537.36 Edge/18.18363"}
    html = requests.get(url, headers=hr).text
    soup = BeautifulSoup(html).find_all("div", attrs={"class":"j-r-list-c"})    # 匹配视频链接标签
    for i in soup:
        a = i("a")
        video_name.append(str(a[0].text))
        url_reg = r'data-mp4="(.*?)"'   # 匹配下载链接
        url_it = re.findall(url_reg, str(i))
        url_name.append(url_it)
    for i in range(len(video_name)):
        urllib.request.urlretrieve(url_name[i][0], r'D:\\Python\\爬虫\\XpathTest\\%s.mp4' % video_name[i])
        print("Downloading :", video_name[i])


if __name__ == '__main__':
    url = "http://www.budejie.com/video/"
    get_html(url)
